﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MapData {
    public class MapDocument {

        private List<MapObject> mapObjects;
        //private List<MapObjectStyle> mapStyles;
        //private mapObjectStyles

        
        /// <summary>
        /// Pre:
        /// Post: Constructs a MapDocument object
        /// </summary>
        public MapDocument() {
            mapObjects = new List<MapObject>();
        }

        /// <summary>
        /// Pre: A map object needs to be created and passed to the function
        /// Post: Adds and stores the passed mapobject in the MapDocument
        /// </summary>
        /// <param name="item"></param>
        public void addMapObject(MapObject item) {
            mapObjects.Add(item);
        }

        /// <summary>
        /// Pre:
        /// Post: Returns all the MapDocument's mapobjects as an array
        /// </summary>
        /// <returns></returns>
        public MapObject[] getMapObjects() {
            return mapObjects.ToArray();
        }

        /// <summary>
        /// Pre : true
        /// Post : Tries to find a mapobject which matches name, then removes it
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public void removeMapObject(string name) {
            if (name == null) {
                return;
            }
            int num = 0;
            while ((num = isInList(name)) >= 0) { //Loop it incase more than 1 object has that name
                mapObjects.RemoveAt(num);
            }
        }

        /// <summary>
        /// Pre : true
        /// Post : Removes any mapobjects which isn't in the list
        /// </summary>
        /// <param name="list"></param>
        public void removeItemsNotInList(string[] list) {
            if (list == null) {
                return;
            } else if (list.Length == 0) {
                return;
            }
            int i = 0;
            for(i = 0; i<list.Length; i++) {
                list[i] = list[i].ToLower();
            }
            bool found = false;
            for (i = mapObjects.Count - 1; i >= 0; i--) {
                found = false;
                for (int j = 0; j < list.Length; j++) {
                    if (mapObjects[i].Name.ToLower().Equals(list[j])) {
                        found = true;                        
                        break;
                    }
                }
                if (!found) {
                    mapObjects.RemoveAt(i);
                }
            }
        }

        /// <summary>
        /// Tries to find a mapObject in the list that matches with provided name
        /// </summary>
        /// <param name="name">Name of the map object you want to find</param>
        /// <returns>-1 if not found, otherwise returns the positon in the list where it can be found</returns>
        private int isInList(string name) {
            if (name == null) {
                return -1;
            }
            name = name.ToLower();
            for (int i = 0; i < this.mapObjects.Count; i++) {
                if (mapObjects[i].Name.ToLower().Equals(name)) {
                    return i;
                }
            }
            return -1;
        }
    }
}
